﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace DreamBox.DAL.Admin
{
    public class GetCompetence
    {
        public DataTable GetList()
        {
            return ParentItems();
        }
        //一级权限
        private DataTable ParentItems()
        {
            DataTable newDt = new DataTable();
            DAL.PSelect ps = new PSelect();
            DataTable dt = ps.Select("dbox_UserCompetence", "*", "");
            //
            newDt = dt.Copy();
            newDt.Clear();
            //
            foreach (DataRow row in dt.Rows)
            {
                if (row["ParentID"].ToString() == "0")
                {
                    DataRow newRow = newDt.NewRow();
                    newRow["ComID"] = row["ComID"];
                    newRow["CompetenceName"] = row["CompetenceName"];
                    newRow["Url"] = row["Url"];
                    newRow["IsType"] = row["IsType"];
                    newRow["ParentID"] = row["ParentID"];
                    newDt.Rows.Add(newRow);

                    SubItems(newDt,dt,row["ComID"].ToString());
                }
            }
            return newDt;
        }
        //二级权限
        private void SubItems(DataTable newDt, DataTable dt, string parentID)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["ParentID"].ToString() != "0" && row["ParentID"].ToString() == parentID)
                {
                    DataRow newRow = newDt.NewRow();
                    newRow["ComID"] = row["ComID"];
                    newRow["CompetenceName"] = GetNbsp(int.Parse(row["NodeCount"].ToString())) + "├" + row["CompetenceName"];
                    newRow["Url"] = row["Url"];
                    newRow["IsType"] = row["IsType"];
                    newRow["ParentID"] = row["ParentID"];
                    newDt.Rows.Add(newRow);
                    SubItems(newDt, dt, row["ComID"].ToString());
                }
            }
        }
        string GetNbsp(int Count)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 1; i <= Count; i++)
            {
                str.Append("&nbsp;&nbsp;");
            }
            return System.Web.HttpContext.Current.Server.HtmlDecode(str.ToString());
        }
    }
}
